﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vant.CellTypes;
namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// Interaction logic for UploadDrawingObject.xaml
    /// </summary>
    public partial class UploadDrawingObject : UserControl
    {
        public UploadDrawingObject(IDrawingHelper drawingHelper, Upload cellType)
        {
            var viewModel = new UploadDrawingObjectViewModel();

            var imageCount = cellType.maxCount ?? 20;

            var size = drawingHelper.GetImageCellSize();
            var allowAdd = !cellType.ReadOnly && cellType.showUpload;
            var allowDelete = !cellType.IsDisabled && !cellType.ReadOnly && cellType.deletable;
            int colCount = (int)Math.Floor(size.Width / (cellType.previewSize + 8));
            int rowCount = (int)Math.Floor(size.Height / (cellType.previewSize + 8));
            colCount = Math.Max(colCount, 1);
            rowCount = Math.Max(rowCount, 1);

            imageCount = Math.Min(colCount * rowCount - (allowAdd ? 1 : 0), imageCount);

            for (int i = 0; i < imageCount; i++)
            {
                viewModel.UploadItems.Add(new UploadItem()
                {
                    Width = cellType.previewSize,
                    Height = cellType.previewSize,
                    CloseButtonVisibility = allowDelete ? Visibility.Visible : Visibility.Collapsed,
                    CloseIcon = IconHelper.GetIcon(new ImageValue()
                    {
                        BuiltIn = true,
                        Name = "Mobile/456_delete_8.svg",
                        Color = "White"
                    }, drawingHelper),
                    ImageVisibility = Visibility.Visible,
                    Stretch = GetImageStretch(cellType.imageFit, cellType.previewSize)
                });
            }

            if(allowAdd)
            {
                viewModel.UploadItems.Add(new UploadItem()
                {
                    Width = cellType.previewSize,
                    Height = cellType.previewSize,
                    CloseButtonVisibility = Visibility.Collapsed,
                    ImageVisibility = Visibility.Collapsed,
                    Icon = IconHelper.GetIcon(cellType.uploadIcon, drawingHelper) ?? IconHelper.GetIcon(new ImageValue()
                    {
                        BuiltIn = true,
                        Name = "Mobile/421_camera_1.svg",
                        Color = "#dcdee0"
                    }, drawingHelper),
                    TextVisibility = string.IsNullOrEmpty(cellType.uploadText) ? Visibility.Collapsed : Visibility.Visible,
                    Text = cellType.uploadText,
                    NewIconVisiblity = Visibility.Visible
                });
            }
            if (cellType.IsDisabled)
            {
                this.Opacity = 0.5;
            }

            this.DataContext = viewModel;
            InitializeComponent();
        }

        private Stretch GetImageStretch(string imageFit, int previewSize)
        {
            //fill|contain|cover|initial|scale-down
            switch (imageFit)
            {
                case "fill":
                    return Stretch.Fill;
                case "contain":
                    return Stretch.Uniform;
                case "cover":
                    return Stretch.UniformToFill;
                case "initial":
                    return Stretch.None;
                case "scale-down":
                default:
                    return previewSize > 64 ? Stretch.None : Stretch.Uniform;
            }
        }
    }
    public class UploadDrawingObjectViewModel 
    {
        public List<UploadItem> UploadItems { get; set; } = new List<UploadItem>();
    }

    public class UploadItem
    {
        public double Opacity { get; set; } = 1;
        public double Width { get; set; }
        public double Height { get; set; }

        public object Icon { get; set; }
        public object CloseIcon { get; set; }
        public Visibility ImageVisibility { get; set; }
        public Visibility CloseButtonVisibility { get; set; }
        public Stretch Stretch { get; set; }
        public Visibility TextVisibility { get; set; } = Visibility.Collapsed;
        public Visibility NewIconVisiblity { get; set; } = Visibility.Collapsed;
        public string Text { get; set; }
    }
}
